package com.kakaxi.mybilibili.module.search.presenter;

import com.kakaxi.mybilibili.adapter.entity.HorizontalVideoMultiItem;
import com.kakaxi.mybilibili.bean.HorizontalVideoItemBean;
import com.kakaxi.mybilibili.module.search.model.ISearchModel;
import com.kakaxi.mybilibili.module.search.model.SearchModel;
import com.kakaxi.mybilibili.module.search.view.ISearchView;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索控制层实现类
 * Created by kakaxi on 16/11/25.
 */

public class SearchPresenter implements ISearchPresenter {

    private ISearchView mView;

    private ISearchModel mModel;

    public SearchPresenter(ISearchView searchView) {
        mView = searchView;
        mModel = new SearchModel();
    }

    @Override
    public void searchVideoData(String keyword, int page) {
        mView.showLoading();
        mModel.getSearchVideoData(keyword, String.valueOf(page), null, new Listener<String>() {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                mView.hideLoading();
                if (response == null || response.isEmpty()) {
                    mView.showMessage("搜索失败,请重试!");
                    return;
                }

                mView.showSearchVideo(parseVideoData(response));
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
                mView.showMessage(error.getMessage());
            }
        });
    }


    private List<HorizontalVideoMultiItem> parseVideoData(String response) {
        List<HorizontalVideoMultiItem> videoMultiItemList = new ArrayList<>();
        Document doc = Jsoup.parse(response);

        // 视频总数
        String totalStr = doc.getElementsByClass("so-info-total").get(0).text();
        videoMultiItemList.add(new HorizontalVideoMultiItem(HorizontalVideoMultiItem.TYPE_HEADER,
                totalStr));

        // 所有视频
        Elements videoElements = doc.getElementsByClass("video");
        for (int i = 0; i < videoElements.size(); i++) {
            HorizontalVideoItemBean itemBean = new HorizontalVideoItemBean();
            Element videoElement = videoElements.get(i);

            // 第一个子标签, a链接
            Element aElement = videoElement.child(0);
            // 网站的视频链接
            String arcurl = aElement.attr("href");
            itemBean.setArcurl(arcurl);
            // av号
            String avId = arcurl.substring(arcurl.indexOf("av") + 2, arcurl.length());
            itemBean.setId(Integer.valueOf(avId));
            // 标题
            String title = aElement.attr("title");
            itemBean.setTitle(title);
            // 封面
            String pic = aElement.getElementsByTag("img").get(0).attr("src");
            itemBean.setPic(pic);

            // 第二个子标签, <div class="info"
            Element divElement = videoElement.child(1);
            // 观看数量
            String play = divElement.select("span[title='观看']").get(0).text();
            itemBean.setPlay(play);
            // 弹幕数量
            String dumanku = divElement.select("span[title='弹幕']").get(0).text();
            itemBean.setVideoReview(dumanku);
            // up主
            String name = divElement.select("span[title='up主']").get(0).text();
            itemBean.setAuthor(name);
//            LogUtil.d("视频数:" + divElement.select("span[title='观看']").get(0).text());

            videoMultiItemList.add(new HorizontalVideoMultiItem(HorizontalVideoMultiItem
                    .TYPE_CONTENT, itemBean));
        }

        return videoMultiItemList;
    }

}
